﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Core.Interface;
using System.Xml.Linq;

namespace NFlickr.Core
{
    public abstract class ServiceBase : IFlickrService
    {
        private string _applicationKey;
        public string ApplicationKey
        {
            get
            {
                if (string.IsNullOrEmpty(_applicationKey))
                {
                    return NFlickr.Resource.FlickrService.ApplicationKey;
                }
                return _applicationKey;
            }
            set
            {
                _applicationKey = value;
            }
        }


        private string _secret;

        public string Secret
        {
            get
            {
                if (string.IsNullOrEmpty(_secret))
                {
                    return NFlickr.Resource.FlickrService.Secret;
                }
                return _secret;
            }
            set
            {
                _secret = value;
            }
        }

        public abstract string MethodName
        {
            get;
        }

        public abstract string TagName
        {
            get;
        }

        public abstract bool RequireSigned
        {
            get;
        }



        public virtual string FlickrServiceUrl
        {
            get
            {
                return NFlickr.Resource.Url.ServiceUrl;
            }
        }


        public virtual string GetQueryString(Dictionary<string, string> paramList)
        {
            var resultList = from para in paramList
                             orderby para.Key ascending
                             select new { para.Key, para.Value };



            string result = string.Empty;
            foreach (var item in resultList)
            {
                result += item.Key + "=" + item.Value + "&";
            }

            result = result.Substring(0, result.Length - 1);

            return result;
        }

        public abstract bool Refresh();

        protected IResult ParseResult(string response)
        {
            IResult result = null;

            XDocument document = XDocument.Parse(response);
            XElement rspElement = document.Element("rsp");
            if (rspElement.Attribute("stat").Value.Equals("ok"))
            {
                string[] array = TagName.Split(@"/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                XElement resultElement = rspElement;
                foreach (string keyName in array)
                {
                    resultElement = resultElement.Element(keyName);
                }
                if (resultElement == null)
                {
                    result = new Result { ResultCode = 0, ResultString = string.Empty };
                }
                else
                {
                    result = new Result { ResultCode = 0, ResultString = resultElement.Value };
                }
            }
            else
            {
                XElement errElement = rspElement.Element("err");

                result = new Result { ResultCode = int.Parse(errElement.Attribute("code").Value), ResultString = errElement.Attribute("msg").Value };
            }
            return result;
        }


        protected int GetInteger(string value)
        {
            int result = 0;
            if(!int.TryParse(value,out result))
            {
                return 0;
            }
            return result;
        }

        protected bool GetBoolean(string value)
        {
            return value=="1"?true:false;
        }

        protected string GetString(IList<string> items)
        {
            string result = string.Empty;
            if (items.Count > 100)
            {
                StringBuilder sb = new StringBuilder(items.Count);
                foreach (string item in items)
                {
                    sb.Append(item);
                    sb.Append(",");
                }
                result = sb.ToString();
            }
            else
            {
                foreach (string item in items)
                {
                    result += item + ",";
                }
            }
            if (result.EndsWith(","))
            {
                return result.Substring(0, result.Length - 1);
            }
            return result;
        }

        #region IFlickrService Members

        public Dictionary<string, string> Params
        {
            get;
            set;
        }


        #endregion
    }
}
